#! /bin/bash
if [ -n "$APTPROXY" ]; then
ainsl -a /etc/apt/apt.conf.d/02proxy "Acquire::http::Proxy \"$APTPROXY\";"
ainsl  /etc/apt/apt.conf.d/02proxy 'Acquire::https::proxy "DIRECT";'
else
    rm -f $target/etc/apt/apt.conf.d/02proxy
fi
echo "DPkg::Progress-Fancy "true";" > $target/etc/apt/apt.conf.d/01color
echo "APT::Color "true";" >> $target/etc/apt/apt.conf.d/01color
echo force-unsafe-io > $target/etc/dpkg/dpkg.cfg.d/unsafe-io

$ROOTCMD apt-get update -y
$ROOTCMD apt-get install -y apt-transport-https

#kernel modules for zfs
ainsl -a /etc/dkms/spl.conf 'POST_INSTALL="scripts/dkms.postbuild -a ${arch} -k ${kernelver} -v ${PACKAGE_VERSION} -n ${PACKAGE_NAME} -t ${dkms_tree}"'

ainsl -a /etc/apt/apt.conf.d/03download 'Acquire::Queue-Mode "access";'
ainsl /etc/apt/apt.conf.d/03download 'Acquire::Retries "3";'
ainsl /etc/apt/apt.conf.d/03download 'Acquire::CompressionTypes::Order "lzma";'
ainsl /etc/apt/apt.conf.d/03download 'Acquire::ForceIPv4 "true";'
# you may want to add i386 arch to amd64 hosts
if ifclass AMD64; then
    $ROOTCMD dpkg --add-architecture i386
fi
cp -f /etc/apt/trusted.gpg.d/*.gpg $target/etc/apt/trusted.gpg.d/

# use external mirror, remove this script when using a mirror from CD

cat <<EOM > $target/etc/apt/sources.list
# external mirror
deb MIRRORURL $ubuntudist main restricted universe multiverse
deb MIRRORURL $ubuntudist-updates main restricted universe multiverse
deb MIRRORURL $ubuntudist-security main restricted universe multiverse
deb MIRRORURL $ubuntudist-backports main restricted universe multiverse
#deb [arch=amd64] https://download.docker.com/linux/ubuntu $ubuntudist stable
deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $ubuntudist stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu $ubuntudist stable

#Nvidia Docker
#deb https://nvidia.github.io/libnvidia-container/ubuntu16.04/amd64 /
#deb https://nvidia.github.io/nvidia-container-runtime/ubuntu16.04/amd64 /
#deb https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64 /

# Ceph packages
deb http://mirrors.aliyun.com/ceph/debian-mimic/ $ubuntudist main

#kubernetes
#deb http://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
#deb http://ppa.launchpad.net/ansible/ansible/ubuntu $ubuntudist main

#The official MongoDB repository
#deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse
#deb [ arch=amd64,arm64 ] http://mirrors.aliyun.com/mongodb/apt/ubuntu xenial/mongodb-org/3.6 multiverse

#Elasticsearch
#deb https://artifacts.elastic.co/packages/5.x/apt stable main

#graylog
#deb https://packages.graylog2.org/repo/debian/ stable 2.4

#Oracle Java 8
#deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main
#deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main

#Ambari VERSION_NUMBER=2.6.1.5-3
#deb http://public-repo-1.hortonworks.com/ambari/ubuntu16/2.x/updates/2.6.1.5 Ambari main
#HDP VERSION_NUMBER=2.6.4.0-91
#deb http://public-repo-1.hortonworks.com/HDP/ubuntu16/2.x/updates/2.6.4.0 HDP main
#deb http://public-repo-1.hortonworks.com/HDP-UTILS-1.1.0.22/repos/ubuntu16 HDP-UTILS main

# Packages for Cloudera Manager, Version 5, on Ubuntu 16.04 amd64       
#deb [arch=amd64] http://archive.cloudera.com/cm5/ubuntu/xenial/amd64/cm xenial-cm5 contrib
#deb-src http://archive.cloudera.com/cm5/ubuntu/xenial/amd64/cm xenial-cm5 contrib

# Packages for Cloudera's Distribution for Hadoop, Version 5, on Ubuntu 16.04 amd64       
#deb [arch=amd64] http://archive.cloudera.com/cdh5/ubuntu/xenial/amd64/cdh xenial-cdh5 contrib
#deb-src http://archive.cloudera.com/cdh5/ubuntu/xenial/amd64/cdh xenial-cdh5 contrib

EOM

if ifclass CUDA; then
    sed -i 's/^#deb\ https:\/\/nvidia.github.io/deb\ https:\/\/nvidia.github.io/' $target/etc/apt/sources.list
fi

# determine a fast mirror for Ubuntu
list=$(curl -s http://mirrors.ubuntu.com/mirrors.txt)
mirror=$(netselect $list | awk '{print $2}')
sed -i -e "s#MIRRORURL#$mirror#" $target/etc/apt/sources.list
